package fr.mywiki.business.entry;

import java.rmi.RemoteException;

import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;

import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.model.ejbAdapter.Value;


public class EntryLite extends Value {
	private static final long serialVersionUID = 1L;

	/** The type of this object */
	public static final String TYPE_ENT = "entry";
	
	/* The fields constants */
	public static final String FIELD_INTERN_TITLE = "intern_title";
	public static final String FIELD_TITLE = "title";
	public static final String FIELD_CURRENT_VERSION = "id_version";
  public static final String FIELD_LOCKTIME = "lock";
	
	/* The links constants */
	public static final String LINK_VERSION_ENTRY = VersionLite.LINK_VERSION_ENTRY;
	
	/* The fields */
	/** Intern title, useful for easy search and to avoid doublons */
	private String internTitle;
	/** Real title of the entry written the right way. */
	private String title;
	/** Identifier of the current version. */
	private Long currentVersion;
	
	public String getTypeEnt() {
		return TYPE_ENT;
	}
	
	public Object get(String attName) {
		if (FIELD_TITLE.equals(attName))
			return getTitle();
		if (FIELD_INTERN_TITLE.equals(attName))
			return getInternTitle();
		if (FIELD_CURRENT_VERSION.equals(attName))
			return getCurrentVersion();
		return null;
	}

	public void set(String attName, Object value) {
		if (FIELD_INTERN_TITLE.equals(attName))
			setInternTitle((String) value);
		else if (FIELD_TITLE.equals(attName))
			setTitle((String) value);
		else if (FIELD_CURRENT_VERSION.equals(attName))
			setCurrentVersion((Long) value);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getInternTitle() {
		return internTitle;
	}

	public void setInternTitle(String internTitle) {
		this.internTitle = internTitle;
	}
	
	public void update() throws ObjectNotFoundException, RemoteException {
		setInternTitle(EntryManager.getInternTitle(getTitle()));
		super.update();
	}
  
  /**
   * Returns the content of the current version of the entry.
   * 
   * @return a String object
   */
  public String getContent() throws RemoteException, FinderException {
  	return EntryManager.getCurrentContent(this);
  }

	public void setCurrentVersion(Long a_currentVersion) {
		currentVersion = a_currentVersion;
	}

	public Long getCurrentVersion() {
		return currentVersion;
	}
}
